<?php
/**
 * 用户表数据操作层
 * @author yangchao
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2011-01-17
 */
define("CACHE_PREFIX_USER_SEARCH","_qq_user_search_");
class db_qq_user_search extends db_base {
    
	var $table_name = "qq_user_search";
	
	function db_qq_user_search()
	{
		parent::db_base();
	}
	
	/**
	 * 前台取用户信息
	 * @param $uid
	 */
	function get_qq_user_search_item_by_uid_and_topic_id($uid,$topic_id)
	{
		$mc = mem_cache::get_instance();
		$key = CACHE_PREFIX_USER."get_qq_user_search_item_by_uid_and_topic_id-{$uid}-{$topic_id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$content = $this->get_alllist($this->table_name,1,1,"","uid='{$uid}'");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}

	//用户搜索
	function get_user_count_by_search($key="",$province="",$city="",$topic_id="",$sex="",$verified_type="",$school_id="",$start_year="")
	{
		$where_array[] = "topic_id = {$topic_id}";
		if($province)$where_array[] = "province = {$province}";
		if($city)$where_array[] = "city = {$city}";
		if($key)$where_array[] = "user_nickname like '{$key}%' ";
		if(strlen($sex)>0)$where_array[] = "sex = $sex";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type";
		if(strlen($school_id)>0)$where_array[] = "school_id = $school_id";
		if(strlen($start_year)>0)
		{
			$last = $start_year - 3;
			$end = $start_year + 3;
			$where_array[] = " (   start_year >= '{$last}' and $start_year <= '{$end}'   )";
		}
		$where = join( " and " , $where_array);
		
		
		$count = $this->get_listcount($this->table_name,$where);
		
		
		return $count;
		
	}
	
	//用户搜索
	function get_user_list_by_search($p=1,$pcount=20,$key="",$province="",$city="",$topic_id="",$sex="",$verified_type="",$school_id="",$start_year="")
	{
		$where_array[] = "topic_id = {$topic_id}";
		if($province)$where_array[] = "province = {$province}";
		if($city)$where_array[] = "city = {$city}";
		if($key)$where_array[] = "user_nickname like '{$key}%' ";
		if(strlen($sex)>0)$where_array[] = "sex = $sex";
		if(strlen($verified_type)>0)$where_array[] = "verified_type = $verified_type";
		if(strlen($school_id)>0)$where_array[] = "school_id = $school_id";
		if(strlen($start_year)>0)
		{
			$last = $start_year - 3;
			$end = $start_year + 3;
			$where_array[] = " (   start_year >= '{$last}' and $start_year <= '{$end}'   )";
		}
		
		$where = join( " and " , $where_array);
		
		
		$list = $this->get_alllist($this->table_name,$p,$pcount,"topic_count desc,topic_text_count",$where);
		
		
		return $list;
		
	}
	
	
	/**
	 * 用户搜索数据初始化
	 * @param $user_name
	 * @param $password
	 */
	function insert_qq_user_search($openid)
	{
		
		$ary["uid"] = $openid;
		$ary["status"] = 1;
	
		$ret = $this->insert($this->table_name, $ary);
		

		return $ret;
	}
	
	

	
	//更新用户信息
	function update_qq_user_search_by_uid($uid,$topic_id,$topic_count,$topic_video_count,$topic_text_count)
	{
		$ary = array();

		if($topic_count)
		{
			$ary["topic_count"] = "topic_count+{$topic_count}";
		}
		if($topic_video_count)
		{
			$ary["topic_video_count"] = "topic_video_count+{$topic_video_count}";
		}
		if($topic_text_count)
		{
			$ary["topic_text_count"] = "topic_text_count+{$topic_text_count}";
		}
		
		$where = "uid = $uid and topic_id = $topic_id";
		
		
		$ret = $this->update($this->table_name, $ary,$where);
	
		return $ret;
		
		
		
	}
	
	
	

	/**
	 * 前台取用户信息
	 * @param $uid
	 */
	function cron_get_qq_user_search_item_by_uid_and_topic_id($uid,$topic_id)
	{
		
		$content = $this->get_alllist($this->table_name,1,1,"","uid='{$uid}' and topic_id = $topic_id");
			
		
		return $content[0];
		
	}
	
	
	
		//更新用户信息
	function cron_update_qq_user_search_by_uid($uid,$topic_id,$start_id="",$video_start_id="",$text_start_id="")
	{
		$ary = array();

		
		if(!$uid || !$topic_id)
		{
			var_dump($uid);
			return false;
		}
		if($start_id)
		{
			$ary["start_id"] = $start_id;
		}
		
		if($video_start_id)
		{
			$ary["video_start_id"] = $video_start_id;
		}
		if($text_start_id)
		{
			$ary["text_start_id"] = $text_start_id;
		}
		
		
		$where = "uid = $uid and topic_id = $topic_id";
		
		
		$ret = $this->update($this->table_name, $ary,$where);
	
		return $ret;
		
		
		
	}
	
	
}



